package com.yycx.module.pay.provider.strategy;

import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.http.UriVariables;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.module.pay.client.enums.PayTypeEnum;
import com.yycx.module.pay.provider.response.PayResponse;
import com.yycx.module.pay.provider.strategy.base.BasePayStrategy;
import com.yycx.module.pay.provider.utils.PayUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 支付宝支付参数获取实现
 *
 * @author zyf
 */
@Component
public class AliPayStrategy implements BasePayStrategy {
    @Override
    public boolean support(String payType, String transactionType) {
        if (payType.equals(PayTypeEnum.aliPay.name())) {
            return true;
        }
        return false;
    }

    @Override
    public ResultBody getPayResult(ResultBody resultBody, EntityMap result, PayResponse payResponse, PayOrder payOrder, String transactionType) {
        Object payInfo = "";
        //APP支付
        if (AliTransactionType.APP.name().equals(transactionType)) {
            payInfo = UriVariables.getMapToParameters(payResponse.getService().orderInfo(payOrder));
        }
        //二维码支付
        if (AliTransactionType.SWEEPPAY.name().equals(transactionType)) {
            payInfo = PayUtils.getPayQrCode(payResponse, payOrder);
        }
        //网页支付
        if (AliTransactionType.PAGE.name().equals(transactionType)) {
            Map<String, Object> orderInfo = payResponse.getService().orderInfo(payOrder);
            payInfo = payResponse.getService().buildRequest(orderInfo, MethodType.POST);
        }
        //手机网站支付
        if (AliTransactionType.WAP.name().equals(transactionType)) {
            Map<String, Object> orderInfo = payResponse.getService().orderInfo(payOrder);
            payInfo = payResponse.getService().buildRequest(orderInfo, MethodType.POST);
        }
        result.put("payInfo", payInfo);
        return resultBody;
    }

}
